package com.cat.dataStructure;
/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-ii/
 * @create 2025/8/15 09:21
 * @since JDK17
 */

public class Solution12 {
    public int minimumSum(int[] nums) {
        int n = nums.length, ans = Integer.MAX_VALUE, min = nums[0];
        int[] rearmin = new int[n];
        rearmin[n - 1] = nums[n - 1];
        n--;
        for (int i = n - 1; i >= 0; i--) {
            rearmin[i] = Math.min(rearmin[i + 1], nums[i]);
        }
        for (int i = 1; i < n; i++) {
            if (nums[i] > min && nums[i] > rearmin[i + 1]) {
                ans = Math.min(ans, min + nums[i] + rearmin[i + 1]);
            }
            min = Math.min(min, nums[i]);
        }
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }
}
